package com.x.chat.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

@Controller
public class GetImageController {
    @Value("${image-path}")
    private String imagePath;

    @GetMapping(value = "/getImage/{imageId}")
    public void getImage(@PathVariable("imageId") String imageId, HttpServletResponse response) throws IOException {
        OutputStream os = response.getOutputStream();
        response.setContentType("image/png");
        File image = new File(imagePath + File.separator + imageId);
        StreamUtils.copy(new FileInputStream(image), os);
    }
}
